﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Collections;
using System.IO;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.User.Identity.IsAuthenticated)
            Response.Redirect("login.aspx");

        if (Roles.IsUserInRole("超级管理员"))
            Response.Redirect("index.aspx");

        if (!Page.IsPostBack)
        {
            int i;
            ArrayList alYear = new ArrayList();
            ArrayList alMonth = new ArrayList();
            ArrayList alDay = new ArrayList();

            for (i = DateTime.Now.Year; i > 1900; i--)
                alYear.Add(i.ToString());

            for (i = 1; i <= 12; i++)
                alMonth.Add(i.ToString());

            for (i = 1; i < 32; i++)
                alDay.Add(i.ToString());

            ddlYear.DataSource = alYear;
            ddlYear.DataBind();

            ddlMonth.DataSource = alMonth;
            ddlMonth.DataBind();

            ddlDay.DataSource = alDay;
            ddlDay.DataBind();

            string strsql = "SELECT * FROM [member] where [user]='" + Page.User.Identity.Name + "'";
            SqlDataReader sdr = SQLHelper.SQLHelper.ExecReader(strsql);
            if (sdr.Read())
            {
                try
                {
                    //DropDownListClass.Text = sdr["class"].ToString();
                }
                catch
                {
                }
                 TextBoxName.Text = sdr["name"].ToString();
                DropDownListSex.Text = sdr["sex"].ToString();
                try
                {
                    DateTime birth = DateTime.Parse(sdr["birth"].ToString());
                    ddlDay.Text = birth.Day.ToString();
                    ddlMonth.Text = birth.Month.ToString();
                    ddlYear.Text = birth.Year.ToString();
                }
                catch
                { }
                TextBoxAddress.Text = sdr["address"].ToString();
                TextBoxPhone.Text = sdr["phone"].ToString();
                TextBoxEmail.Text = sdr["email"].ToString();
                Image1.ImageUrl = sdr["image"].ToString();
                
            }

        }




       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Button7_Click(object sender, EventArgs e)
    {

    }

    protected void Button5_Click(object sender, EventArgs e)
    {

    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        string _user = Page.User.Identity.Name;
        string _name = TextBoxName.Text;
        string _sex = DropDownListSex.Text;
        string _birth = new DateTime(int.Parse(ddlYear.Text), int.Parse(ddlMonth.Text), int.Parse(ddlDay.Text)).ToLongDateString();
        string _image = DateTime.Now.ToFileTime().ToString();
        string _imageurl = "upload/" + _image;

        string _address = TextBoxAddress.Text;
        string _phone = TextBoxPhone.Text;
        string _email = TextBoxEmail.Text;
        string _class = DropDownListClass.Text;



      


        //上传头像
        Boolean fileOK = false;
        String path = Server.MapPath("upload/");//可考虑用server.path来存储相对地址
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        if (FileUpload1.HasFile)
        {
            String fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(path
                    + _image);
            }
            catch
            {
                Label1.Text = "头像上传失败!";
                return;
            }
        }

        //添加联系人
        try
        {
            string strsql = "delete from member where [user]='" + User.Identity.Name + "'";
            SQLHelper.SQLHelper.ExecNonQuery(strsql);
            strsql = "insert into member ([user],[name],[sex],[birth],[address],[phone],[email],[class],[image]) values ('" + User.Identity.Name + "','" + _name + "','" + _sex + "','" + _birth + "','" + _address + "','" + _phone + "','" + _email + "','" + _class + "','" + _imageurl + "')";
            SQLHelper.SQLHelper.ExecNonQuery(strsql);
        }
        catch
        {
            Label1.Text = "添加联系人失败!";
            return;
        }



        Response.Redirect("UserInfo.aspx");
    }
}